﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.IO;
using System.Collections.Generic;
using System.Text;

namespace NasuTek.Register.Shion
{
    public partial class Exception : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Request["exception"] != null)
            {
                File.AppendAllText("C:\\Shion\\ReportedExceptions.txt", "=NEW EXCEPTION , " + DateTime.Now.ToShortTimeString() + "=" + Environment.NewLine);
                List<byte> bytes = new List<byte>();
                foreach (string i in Request["exception"].Split(','))
                {
                    bytes.Add(Convert.ToByte(i));
                }
                File.AppendAllText("C:\\Shion\\ReportedExceptions.txt", Encoding.ASCII.GetString(bytes.ToArray()));
                File.AppendAllText("C:\\Shion\\ReportedExceptions.txt", Environment.NewLine + "=END EXCEPTION=" + Environment.NewLine);
            }
            else
            {
                TreeView1.Nodes.Clear();
                string[] lines = File.ReadAllLines("C:\\Shion\\ReportedExceptions.txt");
                Dictionary<string, TreeNode> treeNodes = new Dictionary<string, TreeNode>();
                string activedatetime = "";

                foreach (string i in lines)
                {
                    if (i.StartsWith("=NEW EXCEPTION"))
                    {
                        activedatetime = i.Split(',')[1];
                        treeNodes.Add(activedatetime, new TreeNode("Exception " + i.Split(',')[1].Replace("=", "")));
                    }
                    else if (i == "=END EXCEPTION=")
                    {
                        TreeView1.Nodes.Add(treeNodes[activedatetime]);
                    }
                    else
                    {
                        treeNodes[activedatetime].ChildNodes.Add(new TreeNode(i));
                    }
                }
                if (Request.QueryString["date"] != null)
                {
                    foreach (TreeNode i in TreeView1.Nodes)
                    {
                        if (i.Text.Contains(Request.QueryString["date"]))
                        {
                            Label2.Text = "http://hinoki.nasutek.com/shion6reg/exception.aspx?date=" + Request.QueryString["date"].Replace(" ", "%20");
                            foreach (TreeNode j in i.ChildNodes)
                            {
                                TextBox1.Text += j.Text + Environment.NewLine;
                            }

                            break;
                        }
                    }
                }
            }
        }

        protected void TreeView1_SelectedNodeChanged(object sender, EventArgs e)
        {
            TextBox1.Text = "";
            Label2.Text = "http://hinoki.nasutek.com/shion6reg/exception.aspx?date=" + TreeView1.SelectedNode.Text.Replace("Exception", "").Remove(0,1).Replace(" ", "%20");
            foreach (TreeNode i in TreeView1.SelectedNode.ChildNodes)
            {
                TextBox1.Text += i.Text + Environment.NewLine;
            }
        }
    }
}
